package com.ssi.myshop.ui.adapter;

import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ssi.myshop.R;
import com.ssi.myshop.bean.CategoryBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CategoryLeftAdapter extends RecyclerView.Adapter<CategoryLeftAdapter.InnerHolder> {
    private static final String TAG = "CategoryLeftAdapter";

    List<CategoryBean> mData = new ArrayList<>();
    private OnLeftItemClickListener mListener = null;
    private int mCurrentSelectedPosition = 0;


    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_left_item, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        CategoryBean categoryBean = mData.get(position);
        CategoryBean bean = mData.get(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: ");
                if (mListener != null && mCurrentSelectedPosition != position) {
                    mCurrentSelectedPosition = position;
                    mListener.onLeftItemClick(bean);
                    notifyDataSetChanged();
                }
            }
        });
        holder.setData(categoryBean, position);

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.left_category_tv)
        TextView mTextView;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(CategoryBean categoryBean, int position) {
            mTextView.setText(categoryBean.getName());
            if (mCurrentSelectedPosition == position) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mTextView.setBackgroundColor(mTextView.getResources().getColor(R.color.colorEEEEEE, null));
                }
            } else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mTextView.setBackgroundColor(mTextView.getResources().getColor(R.color.white, null));
                }
            }

        }
    }

    /**
     * 设置数据
     *
     * @param data
     */
    public void setCategoryData(List<CategoryBean> data) {
//        mData.clear();
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }

        if (mData.size() > 0 && mListener != null) {
            mListener.onLeftItemClick(mData.get(mCurrentSelectedPosition));
        }
    }

    public void setOnLeftItemClickListener(OnLeftItemClickListener listener) {
        this.mListener = listener;
    }

    public interface OnLeftItemClickListener {
        void onLeftItemClick(CategoryBean bean);
    }

}
